package ru.msu.phys.cmp.mpga.domain.image;

import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U;
import static com.googlecode.javacv.cpp.opencv_core.cvGet2D;
import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_BGR2GRAY;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCvtColor;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvSobel;

import java.util.ArrayList;

import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class ImageProc {

    private static ImageProc instance;
    private static int  width;
    private static int height;
    private static ArrayList<Integer> array;

	private ImageProc (String path){
    }

    public static ImageProc readFile(String path){
            if (instance == null){
                    instance = new ImageProc(path);
            }
            IplImage iploriginal = cvLoadImage(path);
            IplImage srcimg = IplImage.create(iploriginal.width(),iploriginal.height(), IPL_DEPTH_8U, 1);
            IplImage destimg  = IplImage.create(srcimg.width(),srcimg.height(), srcimg.depth(), 1);
            cvCvtColor(iploriginal, srcimg, CV_BGR2GRAY);
            cvSobel(srcimg, destimg,0,1,3);   
            array = new ArrayList<Integer>();
            width = destimg.width();
            height = destimg.height();
            for (int i = 0; i < destimg.height(); i++) {
            	for (int j = 0; j < destimg.width(); j++) {
            		CvScalar s = cvGet2D(destimg, i, j);
            		array.add((int) s.val(0));
            	}
            }
            return instance;
    }
    
	public ArrayList<Integer> getArray(){
		return array;
	}
	public int getWidth() {
		return width;
	}
	
	public int getHeight() {
		return height;
	}
	
}
